package com.company.cloud.customer.client;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;

import com.company.cloud.common.bean.BaseClientResult;
import com.company.cloud.common.bean.BaseResult;
import com.company.cloud.customer.modules.cache.models.AddrModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.company.cloud.common.constant.BaseConstant.EnabledEnum;
import com.company.cloud.common.constant.BaseReturnEnum;
import com.company.cloud.common.exception.BusinessException;
import com.company.cloud.common.utils.CheckUtils;
import com.company.cloud.customer.modules.api.response.AddrResponse;
import com.company.cloud.customer.modules.api.service.CustomerRemoteService;
import com.company.cloud.customer.modules.base.entity.CustomerAddrInfo;
import com.company.cloud.customer.modules.base.service.CustomerAddrInfoService;
import com.company.cloud.customer.modules.cache.utils.CustomerConvertUtils;

@RestController
@Service
public class CustomerRemoteServiceImpl implements CustomerRemoteService {

	@Autowired
	private CustomerAddrInfoService customerAddrInfoService;

	@Override
	public BaseClientResult<List<AddrResponse>> getAddrList(Integer userId) {
		try {
			List<CustomerAddrInfo> addrList = customerAddrInfoService.list(new QueryWrapper<CustomerAddrInfo>()
					.eq("rows_state",EnabledEnum.ENABLED.getCode())
					.eq("user_id", userId)
			);
			List<AddrResponse> result = new ArrayList<AddrResponse>();
			if (!CollectionUtils.isEmpty(addrList)) {
				addrList.forEach(addr->{
					result.add(CustomerConvertUtils.getAddrResponse(addr));
				});
			}
			return BaseClientResult.success(result);
		} catch (Exception e) {
			return BaseClientResult.error(e);
		}
	}

	@Override
	public BaseClientResult<AddrResponse> getAddrById(Integer id) {
		try {
			if(!CheckUtils.hasInt(id)) {
				throw new BusinessException(BaseReturnEnum.PARAM_ERROR);
			}
			CustomerAddrInfo addr= customerAddrInfoService.getById(id);
			if(addr==null || EnabledEnum.DISABLED.getCode().equals(addr.getRowsState())) {
				throw new BusinessException(BaseReturnEnum.DATA_NOT_EXISTS);
			}
			return BaseClientResult.success(CustomerConvertUtils.getAddrResponse(addr));
		} catch (Exception e) {
			return BaseClientResult.error(e);
		}
	}



}
